/**
 * File: Simulador_MM
 *
 * @author Sabrina Fernández
 * @author Marilyn Nowacka
 * @author Daniel Bruzual
 * @author Manuel Gómez
 */

import javax.swing.*;
import java.awt.*;

public class Simulador_MM extends JFrame {

	//Declaracion de Variables
    private JPanel Contenedor_Datos;
    private JPanel Contenedor_Memoria;
    private JLabel Creadores;
    private JLabel Daniel;
    private JLabel Label_Hits;
    private JLabel Label_Memoria;
    private JLabel Label_Miss;
    private JLabel Label_RAM;
    private JLabel Manuel;
    private JLabel Marilyn;
    private JTextField Numero_Hits;
    private JTextField Numero_Miss;
    private JPanel Pestana1;
    private JPanel Pestana2;
    private JPanel Pestana3;
    private JLabel Sabrina;
    private JTabbedPane Separador;
    private JComboBox Tamano_RAM;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTextField jTextField1;
    private JTextField jTextField2;

	int Ram_Gap = 6;
	int Default_Vertical_Memory = 282;

	/**
	 * 
	 */
    public Simulador_MM() {
		initComponents();
		//actualizarMemoria(64);
    }

    /** 
	 * Metodo que inicializa todas las variables antes de cargar los valores de los procesos y la memoria
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

		Separador = new JTabbedPane();
        Pestana1 = new JPanel();
        Label_Memoria = new JLabel();
        Contenedor_Memoria = new JPanel();
        Contenedor_Datos = new JPanel();
        Label_RAM = new JLabel();
        Tamano_RAM = new JComboBox();
        jScrollPane1 = new JScrollPane();
        jPanel1 = new JPanel();
        Pestana2 = new JPanel();
        Label_Hits = new JLabel();
        Label_Miss = new JLabel();
        Numero_Hits = new JTextField();
        Numero_Miss = new JTextField();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jTextField1 = new JTextField();
        jTextField2 = new JTextField();
        Pestana3 = new JPanel();
        Creadores = new JLabel();
        Marilyn = new JLabel();
        Sabrina = new JLabel();
        Daniel = new JLabel();
        Manuel = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Manejador de Memoria");

		/* Area de memoria */
        Label_Memoria.setText("Memoria");
        
		Contenedor_Memoria.setBackground(new Color(198, 171, 210));
        Contenedor_Memoria.setPreferredSize(new Dimension(130, 282));
		Contenedor_Memoria.setMinimumSize(new Dimension(130,282));
		Contenedor_Memoria.setMaximumSize(new Dimension(130,282));

        GroupLayout Contenedor_MemoriaLayout = new GroupLayout(Contenedor_Memoria);
        Contenedor_Memoria.setLayout(Contenedor_MemoriaLayout);

        GroupLayout.ParallelGroup elefante = Contenedor_MemoriaLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup okapi = Contenedor_MemoriaLayout.createSequentialGroup();
	
		for (int i = 0; i < 4; i++) {
			JPanel MemBar = new JPanel();
			MemBar.setBackground(new Color(252, 9, 17));
			MemBar.setPreferredSize(new Dimension(130, 66));

            GroupLayout MemBarLayout = new GroupLayout(MemBar);
            MemBar.setLayout(MemBarLayout);
            MemBarLayout.setHorizontalGroup(
				MemBarLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 130, Short.MAX_VALUE)
            );
            MemBarLayout.setVerticalGroup(
				MemBarLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 66, Short.MAX_VALUE)
            );

            elefante.addComponent(MemBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
            okapi.addComponent(MemBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        } 

        Contenedor_MemoriaLayout.setHorizontalGroup(
			Contenedor_MemoriaLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(Contenedor_MemoriaLayout.createSequentialGroup().addGroup(elefante).addContainerGap())
		);

        Contenedor_MemoriaLayout.setVerticalGroup(Contenedor_MemoriaLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(okapi));

        Contenedor_Datos.setBackground(new Color(219, 219, 219));

        Label_RAM.setText("Tamaño de la RAM en KB");

        Tamano_RAM.setModel(new DefaultComboBoxModel(new String[] { "16", "32", "64", "128" }));

		GroupLayout Contenedor_DatosLayout = new GroupLayout(Contenedor_Datos);

		Contenedor_Datos.setLayout(Contenedor_DatosLayout);
			Contenedor_DatosLayout.setHorizontalGroup(
				Contenedor_DatosLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addGroup(Contenedor_DatosLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(Contenedor_DatosLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addGroup(Contenedor_DatosLayout.createSequentialGroup()
							.addGap(12, 12, 12)
							.addComponent(Tamano_RAM, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(Contenedor_DatosLayout.createSequentialGroup()
							.addComponent(Label_RAM, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(135, 135, 135))))
        );
        Contenedor_DatosLayout.setVerticalGroup(
            Contenedor_DatosLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
            .addGroup(Contenedor_DatosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Label_RAM)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Tamano_RAM, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.LINE_AXIS));
		
		// Agregamos los procesos
        for (int i = 0; i < 2; i++) {
            JPanel proceso = new JPanel();
            JLabel proc_label = new JLabel();
            JPanel proc_color_panel = new JPanel();

            proceso.setMinimumSize(new Dimension(18, 18));
            proceso.setLayout(new BoxLayout(proceso, BoxLayout.LINE_AXIS));

            proc_label.setText("P" + i);
            proceso.add(proc_label);

            proc_color_panel.setBackground(new Color(252, 9, 17));
            proc_color_panel.setMaximumSize(new Dimension(20, 20));
            proc_color_panel.setMinimumSize(new Dimension(20, 20));
            proc_color_panel.setPreferredSize(new Dimension(20, 20));

            GroupLayout proc_color_panelLayout = new GroupLayout(proc_color_panel);
            proc_color_panel.setLayout(proc_color_panelLayout);
            proc_color_panelLayout.setHorizontalGroup(
				proc_color_panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE)
			);
            proc_color_panelLayout.setVerticalGroup(
				proc_color_panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE)
            );
            proceso.add(proc_color_panel);
        }

        jScrollPane1.setViewportView(jPanel1);

        GroupLayout Pestana1Layout = new GroupLayout(Pestana1);
        Pestana1.setLayout(Pestana1Layout);
        Pestana1Layout.setHorizontalGroup(
            Pestana1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, Pestana1Layout.createSequentialGroup()
                .addGroup(Pestana1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(Pestana1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(Contenedor_Memoria, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
                    .addGroup(Pestana1Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(Label_Memoria)))
                .addGap(92, 92, 92)
                .addGroup(Pestana1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(Contenedor_Datos, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        Pestana1Layout.setVerticalGroup(
            Pestana1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(Pestana1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(Pestana1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addGroup(GroupLayout.Alignment.TRAILING, Pestana1Layout.createSequentialGroup()
                        .addComponent(Label_Memoria)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Contenedor_Memoria, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE))
                    .addGroup(GroupLayout.Alignment.TRAILING, Pestana1Layout.createSequentialGroup()
                        .addComponent(Contenedor_Datos, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        Separador.addTab("Memoria", new ImageIcon(getClass().getResource("/images/free-memory-53.png")), Pestana1); // NOI18N
        Pestana1.getAccessibleContext().setAccessibleName("Memoria");

        Label_Hits.setText("Número de Hits:");
        Label_Miss.setText("Número de Miss:");
        jLabel1.setText("Porcentaje de Hits:");
        jLabel2.setText("Porcentaje de Miss");

        GroupLayout Pestana2Layout = new GroupLayout(Pestana2);
        Pestana2.setLayout(Pestana2Layout);
        Pestana2Layout.setHorizontalGroup(
            Pestana2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(Pestana2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(Pestana2Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addGroup(Pestana2Layout.createSequentialGroup()
                        .addComponent(Label_Hits)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Numero_Hits, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE))
                    .addGroup(Pestana2Layout.createSequentialGroup()
                        .addComponent(Label_Miss)
                        .addGap(18, 18, 18)
                        .addComponent(Numero_Miss, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)))
                .addGap(95, 95, 95)
                .addGroup(Pestana2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(Pestana2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        Pestana2Layout.setVerticalGroup(
            Pestana2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(Pestana2Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(Pestana2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(Label_Hits)
                    .addComponent(Numero_Hits, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(Pestana2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(Label_Miss)
                    .addComponent(Numero_Miss, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(237, Short.MAX_VALUE))
        );

        Separador.addTab("Resultados", new ImageIcon(getClass().getResource("/images/Results.jpg")), Pestana2); 

        Creadores.setText("Creadores: ");
        Marilyn.setText("Marilyn");
        Sabrina.setText("Sabrina");
        Daniel.setText("Daniel");
        Manuel.setText("Manuel");

        GroupLayout Pestana3Layout = new GroupLayout(Pestana3);
        Pestana3.setLayout(Pestana3Layout);
        Pestana3Layout.setHorizontalGroup(
            Pestana3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, Pestana3Layout.createSequentialGroup()
                .addContainerGap(325, Short.MAX_VALUE)
                .addGroup(Pestana3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(Creadores)
                    .addGroup(Pestana3Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(Pestana3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(Marilyn)
                            .addComponent(Sabrina)
                            .addComponent(Daniel)
                            .addComponent(Manuel))))
                .addGap(275, 275, 275))
        );
        Pestana3Layout.setVerticalGroup(
            Pestana3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(Pestana3Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(Creadores)
                .addGap(24, 24, 24)
                .addComponent(Marilyn)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Sabrina)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Daniel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Manuel)
                .addContainerGap(223, Short.MAX_VALUE))
        );

        Separador.addTab("Creadores", new ImageIcon(getClass().getResource("/images/lindo-elefante-sonrisas.gif")), Pestana3); 

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(Separador, GroupLayout.PREFERRED_SIZE, 688, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(Separador, GroupLayout.PREFERRED_SIZE, 473, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        Separador.getAccessibleContext().setAccessibleName("Buddy System");

        pack();
    }
 
	// Esto es lo que deberia hacer el inicializador de memoria como tal (NO CORRE)
/*	private void actualizarMemoria(int size_ram) {
		int pages, aux, tam_pages;
        Contenedor_Memoria = new JPanel();

		Contenedor_Memoria.setBackground(new Color(198, 171, 210));
        Contenedor_Memoria.setPreferredSize(new Dimension(130, 282));
		Contenedor_Memoria.setMinimumSize(new Dimension(130,282));
		Contenedor_Memoria.setMaximumSize(new Dimension(130,282));

        GroupLayout Contenedor_MemoriaLayout = new GroupLayout(Contenedor_Memoria);
        Contenedor_Memoria.setLayout(Contenedor_MemoriaLayout);

		pages = size_ram / 4;
		aux = Default_Vertical_Memory - Ram_Gap *(pages -1);
		tam_pages = aux / pages;
		
        GroupLayout.ParallelGroup elefante = Contenedor_MemoriaLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup okapi = Contenedor_MemoriaLayout.createSequentialGroup();

		for (int i = 0; i < pages; i++) {
            JPanel MemBar = new JPanel();
            MemBar.setBackground(new Color(200, 10, 20));
            MemBar.setPreferredSize(new Dimension(130, tam_pages));

            GroupLayout MemBarLayout = new GroupLayout(MemBar);
            MemBar.setLayout(MemBarLayout);
            MemBarLayout.setHorizontalGroup(
                MemBarLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 130, Short.MAX_VALUE)
            );
            MemBarLayout.setVerticalGroup(
                MemBarLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 30, Short.MAX_VALUE)
            );

            elefante.addComponent(MemBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
            okapi.addComponent(MemBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        }

        Contenedor_MemoriaLayout.setHorizontalGroup(
			Contenedor_MemoriaLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(Contenedor_MemoriaLayout.createSequentialGroup().addGroup(elefante).addContainerGap())
		);
		
	}  */
 
	/**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Simulador_MM().setVisible(true);
            }
        });
    }

}
